﻿using System;
using System.Collections.Generic;

namespace CSharpPro._1103;

class demo
{
    static void Main(string[] args)
    {
        List<User> userList = new List<User>(); // 泛型集合存储用户
        IUserService userService = new UserServiceImpl(); 
        ShowMainMenu(userService, userList);
    }

    /// <summary>
    /// 显示主菜单并处理用户选择
    /// </summary>
    private static void ShowMainMenu(IUserService userService, List<User> userList)
    {
        while (true)
        {
            Console.Clear();
            Console.WriteLine("===== 主菜单 =====");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 查看所有用户");
            Console.WriteLine("4. 删除用户");
            Console.WriteLine("5. 退出");
            Console.Write("请选择功能 (1-5): ");

            string input = Console.ReadLine()?.Trim();
            if (string.IsNullOrEmpty(input) || !int.TryParse(input, out int choice))
            {
                Console.WriteLine("请输入有效数字，按任意键继续...");
                Console.ReadKey();
                continue;
            }

            switch (choice)
            {
                case 1:
                    Login(userService, userList);
                    break;
                case 2:
                    Register(userService, userList);
                    break;
                case 3:
                    ShowAllUsers(userList);
                    break;
                case 4:
                    DeleteUser(userList);
                    break;
                case 5:
                    Console.WriteLine("感谢使用，再见！");
                    return;
                default:
                    Console.WriteLine("请输入1-5之间的数字，按任意键继续...");
                    Console.ReadKey();
                    break;
            }
        }
    }

    /// <summary>
    /// 用户注册功能（调用Service方法）
    /// </summary>
    private static void Register(IUserService userService, List<User> userList)
    {
        Console.Clear();
        Console.WriteLine("===== 用户注册 =====");

        Console.Write("请输入用户名: ");
        string username = Console.ReadLine()?.Trim() ?? "";

        Console.Write("请输入密码: ");
        string password = userService.ReadPassword();

        Console.Write("请确认密码: ");
        string confirmPassword = userService.ReadPassword();

        if (password != confirmPassword)
        {
            Console.WriteLine("两次密码不一致，注册失败！按任意键继续...");
            Console.ReadKey();
            return;
        }

        User user = new User(username, password);
        if (userService.Register(user, userList))
        {
            Console.WriteLine("注册成功！按任意键返回主菜单...");
        }
        else
        {
            Console.WriteLine("用户名格式非法或已存在，注册失败！按任意键返回主菜单...");
        }
        Console.ReadKey();
    }

    /// <summary>
    /// 用户登录功能（调用Service方法）
    /// </summary>
    private static void Login(IUserService userService, List<User> userList)
    {
        Console.Clear();
        Console.WriteLine("===== 用户登录 =====");

        Console.Write("请输入用户名: ");
        string username = Console.ReadLine()?.Trim() ?? "";

        Console.Write("请输入密码: ");
        string password = userService.ReadPassword();

        User loginUser = userService.Login(username, password, userList);
        if (loginUser != null)
        {
            Console.WriteLine("\n登录成功！即将进入系统首页...");
            Console.WriteLine("按任意键进入系统首页...");
            Console.ReadKey();
            ShowSystemHomePage(loginUser.Username);
        }
        else
        {
            Console.WriteLine("用户名或密码错误，登录失败！按任意键返回主菜单...");
            Console.ReadKey();
        }
    }

    /// <summary>
    /// 模拟系统首页
    /// </summary>
    private static void ShowSystemHomePage(string username)
    {
        Console.Clear();
        Console.WriteLine($"===== 系统首页 =====");
        Console.WriteLine($"欢迎回来，{username}！");
        Console.WriteLine("\n这是模拟的系统首页...");
        Console.WriteLine("\n按任意键返回主菜单...");
        Console.ReadKey();
    }

    /// <summary>
    /// 查看所有用户（foreach循环迭代）
    /// </summary>
    private static void ShowAllUsers(List<User> userList)
    {
        Console.Clear();
        Console.WriteLine("===== 所有用户信息 =====");
        if (userList.Count == 0)
        {
            Console.WriteLine("暂无用户信息！");
        }
        else
        {
            foreach (var user in userList)
            {
                Console.WriteLine($"用户名：{user.Username}，密码：{user.Password}");
            }
        }
        Console.WriteLine("\n按任意键返回主菜单...");
        Console.ReadKey();
    }

    /// <summary>
    /// 删除用户（for循环操作）
    /// </summary>
    private static void DeleteUser(List<User> userList)
    {
        Console.Clear();
        Console.WriteLine("===== 删除用户 =====");
        Console.Write("请输入要删除的用户名：");
        string username = Console.ReadLine()?.Trim() ?? "";

        for (int i = 0; i < userList.Count; i++)
        {
            if (userList[i].Username == username)
            {
                userList.RemoveAt(i);
                Console.WriteLine("删除成功！按任意键返回主菜单...");
                Console.ReadKey();
                return;
            }
        }
        Console.WriteLine("未找到该用户，删除失败！按任意键返回主菜单...");
        Console.ReadKey();
    }
} 